﻿using System;
using System.Collections.Generic;
using System.Text;
using Gammar.Price.DataAccess;
using Gammar.Price.ObjectModel;
using System.Collections.ObjectModel;
using Gammar.Price.Utility.Configuration;
using Longbow.Web.Caching;
using System.ComponentModel;
using System.Globalization;

namespace Gammar.Price.Business
{
    /// <summary>
    /// 搜索分类业务类
    /// </summary>
    [DataObject]
    public class CategoryLogic
    {
        private const string CategoryCacheKey = "Category_All_{0}";
        private const string Category1 = "0E3F50CB-CFE1-49DB-9BF4-2FD98CB039E6";
        private const string Category2 = "1C1D8EBD-CDD0-46F0-BFE0-7D62A497B914";

        /// <summary>
        /// 获得数据库中所有分类集合
        /// </summary>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public static Collection<Sp_CategoryEntity> RetrieveList(Guid? guid)
        {
            Collection<Sp_CategoryEntity> categories = RetrieveAllList(guid);
            while (categories.Count > 15)
                categories.RemoveAt(15);

            return categories;
        }

        public static Collection<Sp_CategoryEntity> RetrieveAllList(Guid? guid)
        {
            Collection<Sp_CategoryEntity> categories = CacheManager.Get(CategoryCacheKey) as Collection<Sp_CategoryEntity>;
            if (categories == null)
            {
                Sp_Category sp = new Sp_Category(guid);
                List<Sp_CategoryEntity> items = new List<Sp_CategoryEntity>(sp.Execute(DatabaseManager.Instance.CurrentDBAccess));
                categories = new Collection<Sp_CategoryEntity>(items);
                CacheManager.Add(string.Format(CultureInfo.InvariantCulture, CategoryCacheKey, guid), categories);
            }
            return categories;
        }

        /// <summary>
        /// 通过分类主键获得分类名称。已使用缓存，性能有很大提高
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static Sp_CategoryEntity RetrieveCategoryByGuid(Guid guid)
        {
            List<Sp_CategoryEntity> categorys = new List<Sp_CategoryEntity>(RetrieveAllList(guid));
            Sp_CategoryEntity ret = categorys.Find(
                delegate(Sp_CategoryEntity ce)
                {
                    return ce.Cat_id.Value == guid;
                });
            return ret;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static string RetrieveHomeLinkByCategoryId(Guid? guid)
        {
            if (guid == null) return "../Index.html";

            string link = string.Empty;
            switch (guid.Value.ToString("D").ToUpperInvariant())
            {
                case Category1:
                    link = "../Index.html";
                    break;
                case Category2:
                    link = "../Index1.html";
                    break;
            }
            return link;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public static int RetrieveHotKeyIndexByCategoryId(Guid guid)
        {
            int index = 0;
            switch (guid.ToString("D").ToUpperInvariant())
            {
                case Category1:
                    index = 0;
                    break;
                case Category2:
                    index = 1;
                    break;
            }
            return index;
        }
    }
}
